package com.fr.chart.legend;

import com.fr.base.FRFont;
import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreConstants;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.base.equals.Equals;
import com.fr.report.io.xml.ReportXMLUtils;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

public class LegendItem
  implements XMLable
{
  private static final long serialVersionUID = -3537790025233710932L;
  public static final int GAP = 2;
  public static final String XML_TAG = "LegendItem";
  private Rectangle2D bounds;
  private String label;
  private LineMarkerIcon lineMarkerIcon;

  public LegendItem()
  {
  }

  public LegendItem(String paramString)
  {
    setLabel(paramString);
  }

  public void setLineMarkerIcon(LineMarkerIcon paramLineMarkerIcon)
  {
    this.lineMarkerIcon = paramLineMarkerIcon;
  }

  public LineMarkerIcon getLineMarkerIcon()
  {
    return this.lineMarkerIcon;
  }

  public void setLabel(String paramString)
  {
    this.label = paramString;
  }

  public String getLabel()
  {
    return this.label;
  }

  public void setBounds(Rectangle2D paramRectangle2D)
  {
    this.bounds = paramRectangle2D;
  }

  public Rectangle2D getBounds()
  {
    return this.bounds;
  }

  public void draw(Graphics paramGraphics, FRFont paramFRFont)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if (getLineMarkerIcon() != null)
    {
      localGraphics2D.translate(this.bounds.getX(), this.bounds.getY());
      getLineMarkerIcon().paint(localGraphics2D, paramFRFont.getSize());
      localGraphics2D.translate(-this.bounds.getX(), -this.bounds.getY());
    }
    if (getLabel() != null)
    {
      Font localFont = localGraphics2D.getFont();
      Paint localPaint = localGraphics2D.getPaint();
      Composite localComposite = localGraphics2D.getComposite();
      localGraphics2D.setFont(paramFRFont);
      localGraphics2D.setPaint(paramFRFont.getForeground());
      localGraphics2D.setComposite(AlphaComposite.getInstance(3, 1.0F));
      GraphHelper.drawString(localGraphics2D, getLabel(), this.bounds.getX() + paramFRFont.getSize() + 2.0D, this.bounds.getY() + this.bounds.getHeight());
      localGraphics2D.setComposite(localComposite);
      localGraphics2D.setPaint(localPaint);
      localGraphics2D.setFont(localFont);
    }
  }

  public Dimension2D prefferedSize(FRFont paramFRFont)
  {
    int i = GraphHelper.getFontMetrics(paramFRFont).getHeight();
    int j = Math.max(i, paramFRFont.getSize());
    int k = (int)GraphHelper.stringWidth(getLabel(), paramFRFont, BaseCoreConstants.DEFAULT_FRC) + paramFRFont.getSize() + 2;
    return new Dimension(k, j);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("LegendItem");
    if (getBounds() != null)
      ReportXMLUtils.writeBounds(paramXMLPrintWriter, getBounds());
    if (getLabel() != null)
      paramXMLPrintWriter.startTAG("Label").attr("label", getLabel()).end();
    if (getLineMarkerIcon() != null)
      getLineMarkerIcon().writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Bounds"))
      {
        this.bounds = ReportXMLUtils.readBounds(paramXMLableReader);
      }
      else if (str1.equals("Label"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("label")) != null)
          setLabel(str2);
      }
      else if (str1.equals("LineMarkerIcon"))
      {
        setLineMarkerIcon((LineMarkerIcon)paramXMLableReader.readXMLObject(new LineMarkerIcon()));
      }
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof LegendItem))
      return false;
    LegendItem localLegendItem = (LegendItem)paramObject;
    if (localLegendItem.getBounds() != null)
    {
      if (localLegendItem.getBounds().equals(getBounds()))
        break label46;
      return false;
    }
    if (getBounds() != null)
      return false;
    label46: return (Equals.equals(localLegendItem.getLineMarkerIcon(), getLineMarkerIcon()));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    LegendItem localLegendItem = new LegendItem();
    return localLegendItem;
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("label", this.label);
    if (this.lineMarkerIcon != null)
      localJSONObject.put("lineMarkerIcon", this.lineMarkerIcon.toJSONObject());
    return localJSONObject;
  }
}